﻿namespace TicTacToe.Core.AI
{
    static partial class Conditions
    {
        static ConditionResult OwnXTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0)
            {
                if (field[x + 1, y, z] == player &&
                    field[x + 2, y, z] == player &&
                    field[x + 3, y, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 1)
            {
                if (field[x + 1, y, z] == player &&
                    field[x + 2, y, z] == player &&
                    field[x - 1, y, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 2)
            {
                if (field[x - 1, y, z] == player &&
                    field[x - 2, y, z] == player &&
                    field[x + 1, y, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 3)
            {
                if (field[x - 1, y, z] == player &&
                    field[x - 2, y, z] == player &&
                    field[x - 3, y, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnYTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (y == 0)
            {
                if (field[x, y + 1, z] == player &&
                    field[x, y + 2, z] == player &&
                    field[x, y + 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (y == 1)
            {
                if (field[x, y + 1, z] == player &&
                    field[x, y + 2, z] == player &&
                    field[x, y - 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (y == 2)
            {
                if (field[x, y - 1, z] == player &&
                    field[x, y - 2, z] == player &&
                    field[x, y + 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (y == 3)
            {
                if (field[x, y - 1, z] == player &&
                    field[x, y - 2, z] == player &&
                    field[x, y - 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (z == 0)
            {
                if (field[x, y, z + 1] == player &&
                    field[x, y, z + 2] == player &&
                    field[x, y, z + 3] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (z == 1)
            {
                if (field[x, y, z + 1] == player &&
                    field[x, y, z + 2] == player &&
                    field[x, y, z - 1] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (z == 2)
            {
                if (field[x, y, z - 1] == player &&
                    field[x, y, z - 2] == player &&
                    field[x, y, z + 1] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (z == 3)
            {
                if (field[x, y, z - 1] == player &&
                    field[x, y, z - 2] == player &&
                    field[x, y, z - 3] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            return ConditionResult.None;
        }

        static ConditionResult OwnXYTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0 && y == 0)
            {
                if (field[x + 1, y + 1, z] == player &&
                    field[x + 2, y + 2, z] == player &&
                    field[x + 3, y + 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 0 && y == 3)
            {
                if (field[x + 1, y - 1, z] == player &&
                    field[x + 2, y - 2, z] == player &&
                    field[x + 3, y - 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 3 && y == 0)
            {
                if (field[x - 1, y + 1, z] == player &&
                    field[x - 2, y + 2, z] == player &&
                    field[x - 3, y + 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 3 && y == 3)
            {
                if (field[x - 1, y - 1, z] == player &&
                    field[x - 2, y - 2, z] == player &&
                    field[x - 3, y - 3, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 1 && y == 1)
            {
                if (field[x + 1, y + 1, z] == player &&
                    field[x + 2, y + 2, z] == player &&
                    field[x - 1, y - 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 1 && y == 2)
            {
                if (field[x + 1, y - 1, z] == player &&
                    field[x + 2, y - 2, z] == player &&
                    field[x - 1, y + 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 2 && y == 1)
            {
                if (field[x - 1, y + 1, z] == player &&
                    field[x - 2, y + 2, z] == player &&
                    field[x + 1, y - 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }
            else if (x == 2 && y == 2)
            {
                if (field[x - 1, y - 1, z] == player &&
                    field[x - 2, y - 2, z] == player &&
                    field[x + 1, y + 1, z] == 0)
                    return ConditionResult.NextToTwoRow;
            }

            return ConditionResult.None;
        }

        static ConditionResult OwnXZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (x == 0 && z == 0)
            {
                if (field[x + 1, y, z + 1] == player &&
                    field[x + 2, y, z + 2] == player &&
                    field[x + 3, y, z + 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 0 && z == 3)
            {
                if (field[x + 1, y, z - 1] == player &&
                    field[x + 2, y, z - 2] == player &&
                    field[x + 3, y, z - 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 3 && z == 0)
            {
                if (field[x - 1, y, z + 1] == player &&
                    field[x - 2, y, z + 2] == player &&
                    field[x - 3, y, z + 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 3 && z == 3)
            {
                if (field[x - 1, y, z - 1] == player &&
                    field[x - 2, y, z - 2] == player &&
                    field[x - 3, y, z - 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 1 && z == 1)
            {
                if (field[x + 1, y, z + 1] == player &&
                    field[x + 2, y, z + 2] == player &&
                    field[x - 1, y, z - 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 1 && z == 2)
            {
                if (field[x + 1, y, z - 1] == player &&
                    field[x + 2, y, z - 2] == player &&
                    field[x - 1, y, z + 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 2 && z == 1)
            {
                if (field[x - 1, y, z + 1] == player &&
                    field[x - 2, y, z + 2] == player &&
                    field[x + 1, y, z - 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (x == 2 && z == 2)
            {
                if (field[x - 1, y, z - 1] == player &&
                    field[x - 2, y, z - 2] == player &&
                    field[x + 1, y, z + 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }

            return ConditionResult.None;
        }

        static ConditionResult OwnYZTwoRow(AIGameField field, int player, int x, int y, int z)
        {
            if (y == 0 && z == 0)
            {
                if (field[x, y + 1, z + 1] == player &&
                    field[x, y + 2, z + 2] == player &&
                    field[x, y + 3, z + 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 0 && z == 3)
            {
                if (field[x, y + 1, z - 1] == player &&
                    field[x, y + 2, z - 2] == player &&
                    field[x, y + 3, z - 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 3 && z == 0)
            {
                if (field[x, y - 1, z + 1] == player &&
                    field[x, y - 2, z + 2] == player &&
                    field[x, y - 3, z + 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 3 && z == 3)
            {
                if (field[x, y - 1, z - 1] == player &&
                    field[x, y - 2, z - 2] == player &&
                    field[x, y - 3, z - 3] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 1 && z == 1)
            {
                if (field[x, y + 1, z + 1] == player &&
                    field[x, y + 2, z + 2] == player &&
                    field[x, y - 1, z - 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 1 && z == 2)
            {
                if (field[x, y + 1, z - 1] == player &&
                    field[x, y + 2, z - 2] == player &&
                    field[x, y - 1, z + 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 2 && z == 1)
            {
                if (field[x, y - 1, z + 1] == player &&
                    field[x, y - 2, z + 2] == player &&
                    field[x, y + 1, z - 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }
            else if (y == 2 && z == 2)
            {
                if (field[x, y - 1, z - 1] == player &&
                    field[x, y - 2, z - 2] == player &&
                    field[x, y + 1, z + 1] == 0)
                    return ConditionResult.NextToDiagonalTwoRow;
            }

            return ConditionResult.None;
        }
    }
}
